var app = getApp();
Page({
    data: {
        statusType: ["待付款", "待发货", "待收货", "待评价", "已完成","已关闭"],
        status:[ "-8","-7","-6","-5","1","0" ],
        currentType: 0,
        tabClass: ["", "", "", "", "", ""]
    },
    statusTap: function (e) {
        var curType = e.currentTarget.dataset.index;
        this.data.currentType = curType;
        this.setData({
            currentType: curType
        });
        this.onShow();
    },
    orderDetail: function (e) {
        wx.navigateTo({
            url: "/pages/my/order_info?order_sn=" + e.currentTarget.dataset.id
        })
    },
    onLoad: function (options) {
        // 生命周期函数--监听页面加载

    },
    onReady: function () {
        // 生命周期函数--监听页面初次渲染完
    },
    onShow: function () {
        var that = this;
        that.getPayOrder();
    },
    onHide: function () {
        // 生命周期函数--监听页面隐藏

    },
    onUnload: function () {
        // 生命周期函数--监听页面卸载

    },
    onPullDownRefresh: function () {
        // 页面相关事件处理函数--监听用户下拉动作

    },
    onReachBottom: function () {
        // 页面上拉触底事件的处理函数

    },
    getPayOrder: function () {
        const that = this;
        wx.request({
            url: app.buildUrl("/my/order"),
            header: app.getRequestHeader(),
            data: {
                status: that.data.status[that.data.currentType]
            },
            success: function (res) {
                const resp = res.data;
                if (resp.code === 200) {
                    that.setData({
                        order_list: resp.data.pay_order_list
                    })
                } else {
                    app.alert({'content': resp.msg})
                    return;
                }
            }
        })
    },
    // 调起支付
    toPay: function (e) {
        const that = this;
        // 这里增加获取用户订阅消息权限，需要将申请的模版id填写进来
        const template_ids = ["3NUQ1Xa5JZsYJxzqzRjqWCqDWC7HE8rOP9hj2wy5Gvc"];
        // 默认不能发送消息，当用户明确选择了允许才可以发。
        let can_send = 0
        const data = {
            order_sn: e.currentTarget.dataset.id,
            can_send: can_send
        };
        wx.requestSubscribeMessage({
            tmplIds: template_ids,
            success: function (res) {
                for (let tmp_id of template_ids) {
                    if (res.hasOwnProperty(tmp_id) && res[tmp_id] === 'accept') {
                        can_send = 1;
                    }
                }
                data['can_send'] = can_send
                // 成功调用支付下单
                that.doPay(data);
            },
            fail: function (res) {
                // 失败调用支付下单
                that.doPay(data);
            }
        })
    },
    //取消订单
    orderCancel: function (e) {
        this.orderOps(e.currentTarget.dataset.id, "cancel", "确认取消订单?");
    },
    //确认订单
    orderConfirm: function (e) {
        this.orderOps(e.currentTarget.dataset.id, "confirm", "确认收到?");
    },
    orderOps: function (order_sn, act, msg) {
        const that = this;
        const params = {
            "content": msg,
            "cb_confirm": function () {
                wx.request({
                    url: app.buildUrl("/order/ops"),
                    header: app.getRequestHeader(),
                    method: 'POST',
                    data: {
                        order_sn,
                        act
                    },
                    success: function (res) {
                        const resp = res.data;
                        app.alert({'content': resp.msg});
                        if (resp.code === 200) {
                            that.getPayOrder();
                        }
                    }
                });
            }
        };
        app.tip(params);
    },
    orderComment: function (e) {
        wx.navigateTo({
            url: "/pages/my/comment?order_sn=" + e.currentTarget.dataset.id
        })
    },
    doPay: function (data) {
        wx.request({
            url: app.buildUrl("/order/pay"),
            header: app.getRequestHeader(),
            method: 'POST',
            data: data,
            success: function (res) {
                const resp = res.data;
                if (resp.code === 200) {
                    const pay_info = resp.data.pay_info;
                    wx.requestPayment({
                        'timeStamp': pay_info.timeStamp,
                        'nonceStr': pay_info.nonceStr,
                        'package': pay_info.package,
                        'signType': 'MD5',
                        'paySign': pay_info.paySign,
                        'success': function (res) {
                            console.log('res', res)
                        },
                        'fail': function (err) {
                            console.log('err', err)
                        }
                    });
                } else {
                    app.alert({'content': resp.msg})
                    return;
                }
            }
        })
    }
})
